package com.well.rpc.listener;

import com.alibaba.fastjson.JSONObject;
import com.well.rpc.handler.ClientHandler;
import com.well.rpc.config.ClientDataConfig;
import com.well.rpc.model.EventType;
import com.well.rpc.model.RegisterModel;
import com.well.rpc.model.TransData;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment;

import static com.well.rpc.config.ClientDataConfig.isStart;


@Slf4j
@ConfigurationProperties(prefix = "com.wd.register")
public class ClientListener implements ApplicationRunner {

    @Value("${com.well.register.nameSpace:public}")
    private String nameSpace;
    @Value("${com.well.register.group:defaultGroup}")
    private String group;
    @Value("${com.well.register.service:${spring.application.name}}")
    private String service;
    @Value("${com.well.register.host}")
    private String host;
    @Value("${com.well.register.port}")
    private Integer port;
    @Value("${com.well.register.enable:true}")
    private boolean enable;
    @Value("${com.well.register.accessKey:}")
    private String accessKey;
    @Value("${com.well.register.enableSSL:false}")
    private Boolean enableSSL;
    @Value("${com.well.register.caPath:}")
    private String caPath;
    @Value("${com.well.register.certPath:}")
    private String certPath;
    @Value("${com.well.register.keyPath:}")
    private String keyPath;

    @Autowired
    Environment environment;


    public void init() {
        if (StringUtil.isNullOrEmpty(service)) {
            throw new NullPointerException("No Service Name!");
        }
        if (StringUtil.isNullOrEmpty(accessKey)) {
            throw new NullPointerException("No AccessKey!");
        }
        TransData transData = new TransData();
        transData.setType(EventType.REGISTER);
        RegisterModel model = new RegisterModel();
        Integer serverPort = environment.getProperty("server.port", Integer.class);
        if (serverPort == null) {
            throw new NullPointerException(" No Server Port!");
        }
        model.setServerPort(serverPort);
        model.setNameSpace(nameSpace);
        model.setGroup(group);
        model.setServerName(service);
        model.setAccessKey(accessKey);
        transData.setJsonData(JSONObject.toJSONString(model));
        ClientDataConfig.setRegisterData(transData);
        ClientDataConfig.setClientSsl(enableSSL, certPath, keyPath, caPath);
    }

    @Override
    public void run(ApplicationArguments args) {
        if(isStart || !enable){
            return;
        }
        init();
        isStart = true;
        log.info("Server Registering.");
        ClientHandler.connectServer(host, port);
    }
}
